package com.jftone.bh.app.action;

import com.jftone.bh.app.config.SysConst;
import com.jftone.bh.app.model.*;
import com.jftone.bh.app.service.AdminService;
import com.jftone.bh.app.service.BaseService;
import com.jftone.bh.app.util.UploadFile;
import org.apache.log4j.Logger;
import org.jftone.annotation.Autowired;
import org.jftone.annotation.Controller;
import org.jftone.exception.ActionException;
import org.jftone.exception.ComponentException;
import org.jftone.exception.DaoException;
import org.jftone.util.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Controller(mapping="/baseAction")
public class NBaseAction extends BaseAction {

	private Logger logger = Logger.getLogger(NBaseAction.class);
	
	@Autowired
	private BaseService service;

	/**
	 * 加载地区数据
	 * @throws ActionException
	 */
	public void loadArea() throws ActionException{
		IData<String, Object> data = getData();
		String superCode = data.getString("superCode", SysConst.ROOT_CODE);
		try {
			List<IData<String, Object>> list = service.queryAreaBySuperCodeFroRedis(superCode);
			this.send(list);
		} catch (Exception e) {
			logger.error("加载地区数据错误"+e.getMessage(), e);
			throw new ActionException("加载地区数据错误"+e.getMessage(), e);
		}
	}

	public void query() throws ActionException {
		IData<String, Object> data = getData();
		String bname = data.getString("bname", "");
		this.putRenderInfo("bname", bname);
		try {
			//设置功能按钮
			setMenuFuncDerictive();
			//设置分页组件
			setPageDerictive();
			putParam("bname", bname);
			putParam(SysConst.QUERY_PARAMS, getParam());

			List<IData<String, Object>> baseList = service.queryBaseList(data, getPage());
			this.putRenderInfo("baseList", baseList);
			this.render("base/base_list.html");
		} catch (Exception e) {
			logger.error("查询基站列表错误" + e.getMessage(), e);
			throw new ActionException("查询基站列表错误" + e.getMessage(), e);
		}
	}

	public void add() throws ActionException {
		NtbBase base = new NtbBase();
		base.setBaseMali(30000);
		base.setVipMali(5000);
		base.setMaxVip(5000);
		base.setResidueVip(5000);
		this.putRenderInfo("base",base);
		this.render("base/base_form.html");
	}

	public void edit() throws ActionException {
		IData<String, Object> data = getData();
		String dataId = data.getString("dataId");
		if(StringUtil.isBlank(dataId) || !StringUtil.isNumber(dataId)){
			this.alertMsg("参数传值错误");
			return;
		}
		try {
			IData<String, Object> inData = new DataMap<String, Object>();
			inData.put("id", Integer.parseInt(dataId));
			NtbBase base = service.getNtbBase(inData);
			inData.clear();
			inData.put("baseId", Integer.parseInt(dataId));
			List<NtbBaseCoach> coachList = service.queryCoachList(inData);
			this.putRenderInfo("base", base);
			this.putRenderInfo("coachList", coachList);
			this.render("base/base_form.html");
		} catch (Exception e) {
			logger.error("编辑基站数据错误"+e.getMessage(), e);
			throw new ActionException("编辑基站信息错误"+e.getMessage(), e);
		}
	}

	public void save() throws ActionException{
		boolean updateFlag = false;
		IData<String, Object> data = getData();
		String bid = data.getString("bid");
		if(!StringUtil.isBlank(bid) && StringUtil.isNumber(bid)){
			updateFlag = true;
		}
		try {
			int resCode = service.saveBase(data, updateFlag);
			if(resCode == 0){
				this.showMsg("基站信息"+(updateFlag? "保存" : "新增")+"成功", "?method=query");
			}else{
				this.errorMsg("基站信息"+(updateFlag? "保存" : "新增")+"失败", "参数错误","参数错误","?method=query");
			}

		} catch (Exception e) {
			logger.error("保存基站信息错误"+e.getMessage(), e);
			throw new ActionException("保存基站信息错误"+e.getMessage(), e);
		}
	}
	public void delete() throws ActionException{
		IData<String, Object> data = getData();
		String dataId = data.getString("dataId", "");
		if(StringUtil.isBlank(dataId) || !StringUtil.isNumber(dataId)){
			this.alertMsg("参数传值错误");
			return;
		}
		try {
			HttpServletRequest request = getRequest();
			int resultCode = service.delete(Integer.parseInt(dataId));
			if(resultCode == 1){
				this.alertMsg("数据错误，请刷新后重试");
				return;
			}
			this.showMsg("删除成功", "?method=query");
		} catch (Exception e) {
			logger.error("删除数据错误"+e.getMessage(), e);
			throw new ActionException("删除数据错误"+e.getMessage(), e);
		}

	}


	public void coach() throws ActionException {
		IData<String, Object> data = getData();
		String dataId = data.getString("dataId");
		if(StringUtil.isBlank(dataId) || !StringUtil.isNumber(dataId)){
			this.alertMsg("参数传值错误");
			return;
		}
		try {
			IData<String, Object> inData = new DataMap<String, Object>();
			inData.put("id", Integer.parseInt(dataId));
			NtbBase base = service.getNtbBase(inData);
			inData.clear();
			inData.put("baseId", Integer.parseInt(dataId));
			List<NtbBaseCoach> coachList = service.queryCoachList(inData);
			this.putRenderInfo("base", base);
			this.putRenderInfo("coachList", coachList);
			this.render("base/base_coach_list.html");
		} catch (Exception e) {
			logger.error("查询基站教练列表错误" + e.getMessage(), e);
			throw new ActionException("查询基站教练列表错误" + e.getMessage(), e);
		}
	}

	public void addcoach() throws Exception {
		IData<String, Object> data = getData();
		String baseId = data.getString("baseId");
		if(StringUtil.isBlank(baseId) || !StringUtil.isNumber(baseId)){
			this.alertMsg("参数传值错误");
			return;
		}
		IData<String, Object> inData = new DataMap<String, Object>();
		inData.put("id", Integer.parseInt(baseId));
		NtbBase base = service.getNtbBase(inData);
		this.putRenderInfo("base", base);
		NtbBaseCoach baseCoach = new NtbBaseCoach();
		baseCoach.setMali(3000);
		this.putRenderInfo("baseCoach", baseCoach);
		this.render("base/base_coach_form.html");
	}

	public void editCoach() throws Exception {
		IData<String, Object> data = getData();
		String baseId = data.getString("baseId");
		String coachId = data.getString("coachId");
		if(StringUtil.isBlank(baseId) || !StringUtil.isNumber(baseId) || StringUtil.isBlank(coachId) || !StringUtil.isNumber(coachId)){
			this.alertMsg("参数传值错误");
			return;
		}

		IData<String, Object> inData = new DataMap<String, Object>();
		inData.put("id", Integer.parseInt(baseId));
		NtbBase base = service.getNtbBase(inData);
		this.putRenderInfo("base", base);
		inData.put("id", Integer.parseInt(coachId));
		NtbBaseCoach baseCoach = service.getNtbBaseCoach(inData);
		this.putRenderInfo("baseCoach", baseCoach);
		this.render("base/base_coach_form.html");
	}

	public void saveCoach() throws ActionException{
		boolean updateFlag = false;
		IData<String, Object> data = getData();
		String baseId = data.getString("baseId");
		String coachId = data.getString("coachId");
		if(StringUtil.isBlank(baseId) || !StringUtil.isNumber(baseId)){
			this.alertMsg("参数传值错误");
			return;
		}
		if(!StringUtil.isBlank(coachId) && StringUtil.isNumber(coachId)){
			updateFlag = true;
		}
		try {
			int resCode = service.saveCoach(data, updateFlag);
			if(resCode == 0){
				this.showMsg("教练信息"+(updateFlag? "保存" : "新增")+"成功", "?method=coach&dataId="+baseId);
			}else{
				this.errorMsg("教练信息"+(updateFlag? "保存" : "新增")+"失败", "参数错误","参数错误","?method=coach&dataId="+baseId);
			}

		} catch (Exception e) {
			logger.error("保存教练信息错误"+e.getMessage(), e);
			throw new ActionException("保存教练信息错误"+e.getMessage(), e);
		}
	}
	public void deleteCoach() throws ActionException{
		IData<String, Object> data = getData();
		String coachId = data.getString("coachId");
		if(StringUtil.isBlank(coachId) || !StringUtil.isNumber(coachId)){
			this.alertMsg("参数传值错误");
			return;
		}
		try {
			int resultCode = service.deleteCoach(Integer.parseInt(coachId));
			this.send(resultCode,"");
		} catch (Exception e) {
			logger.error("删除数据错误"+e.getMessage(), e);
			throw new ActionException("删除数据错误"+e.getMessage(), e);
		}

	}


	public void uploadFile() throws ActionException {
		IData<String, Object> data = getData();
		String imgSrc = data.getString("imgSrc", "");
		Long imgId = data.getLong("imgId", 0L);
		try {
			this.putRenderInfo("imgSize", 5000000);
			putRenderInfo("resultCode", data.getString("resultCode", "-1"));
			putRenderInfo("imgSrc", imgSrc);
			putRenderInfo("imgId", imgId);
			this.render("base/base_upload.html");
		} catch (Exception e) {
			logger.error("上传文件页面加载错误" + e.getMessage(), e);
			throw new ActionException("上传文件页面加载错误" + e.getMessage(), e);
		}
	}

	public void upload() throws Exception {
		Long maxSize = 5000000L;
		String redirectUrl ="baseAction.do?method=uploadFile";
		this.upload(StringUtil.getRandomCode(12, StringUtil.CHARNUM_MODE), UploadFile.FileDir.BASE, maxSize, 10 * 1024, redirectUrl);
	}



}
